package com.ruoyi.screen.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.screen.domain.dto.Register;
import com.ruoyi.screen.domain.dto.ScanDevice;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.screen.domain.Device;
import com.ruoyi.screen.service.IDeviceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 广告屏设备Controller
 * 
 * @author david
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;
    /**
     * 注册广告屏设备，已经注册的返回与设备关联的的素材
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody Register register){
        return success(deviceService.register(register));
    }

    /**
     * 所有广告屏设备列表
     */
    @RequiresPermissions("screen:device:list")
    @GetMapping("/allList")
    public AjaxResult allList()
    {
        List<Device> list = deviceService.allDeviceList();
        return success(list);
    }

    /**
     * 查询广告屏设备列表
     */
    @RequiresPermissions("screen:device:list")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出广告屏设备列表
     */
    @RequiresPermissions("screen:device:export")
    @Log(title = "广告屏设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "广告屏设备数据");
    }

    /**
     * 获取广告屏设备详细信息
     */
    @RequiresPermissions("screen:device:query")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return success(deviceService.selectDeviceByDeviceId(deviceId));
    }

    /**
     * 新增广告屏设备
     */
    @RequiresPermissions("screen:device:add")
    @Log(title = "广告屏设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }
    /**
     * 扫码新增广告屏设备
     */
    @Log(title = "扫码广告屏设备", businessType = BusinessType.INSERT)
    @PostMapping("/scan")
    public AjaxResult scanAdd(@RequestBody ScanDevice scanDevice)
    {
        System.out.println(scanDevice);
        if(!"AdvertScreen".equals(scanDevice.getAppName())){
            return AjaxResult.warn("请使用广告屏APP扫码");
        }
        //判断serial是否重复
        if(StringUtils.isNotEmpty(scanDevice.getSerial()) && deviceService.selectDeviceBySerial(scanDevice.getSerial()) != null){
            return AjaxResult.warn("该设备已存在");
        }
        Device device = new Device();
        device.setDeviceName(scanDevice.getDeviceName());
        device.setSerial(scanDevice.getSerial());
        // 获取当前的登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            // 获取用户基本信息
            SysUser currentUser = loginUser.getSysUser();
            if (StringUtils.isNotNull(currentUser)) {
                device.setUserId(currentUser.getUserId());
                device.setDeptId(currentUser.getDeptId());
                return toAjax(deviceService.insertDevice(device));
            }
        }
        throw new RuntimeException("用户末登录");

    }

    /**
     * 修改广告屏设备
     */
    @RequiresPermissions("screen:device:edit")
    @Log(title = "广告屏设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Device device)
    {
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除广告屏设备
     */
    @RequiresPermissions("screen:device:remove")
    @Log(title = "广告屏设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(deviceService.deleteDeviceByDeviceIds(deviceIds));
    }
}
